// ==UserScript==
// @name        InkBunny New Submission Autocheck-on-open.
// @namespace   https://inkbunny.net/JustLurking
// @description Opens a new tab for submissions in the new submission view autochecks them so you can keep your place more easily.
// @include     https://inkbunny.net/submissionsviewall.php*
// @version     1
// @grant       none
// ==/UserScript==

(() => {

if (!document.location.search.includes ("mode=unreadsubs"))
	return;

const   links = document.evaluate (
		'//a[starts-with (@href, "/s/")]',
		document,
		null,
		XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
		null
	),
	total_links = links.snapshotLength,
	tick_submission = e => {
		const   first_input = document.evaluate (
				'ancestor-or-self::*['+
				'contains (@class, '+
				'"widget_thumbnailCompleteFromSubmission")'+
				']/descendant-or-self::input',
				e.target,
				null,
				XPathResult.ANY_UNORDERED_NODE_TYPE,
				null
			),
			checkbox = first_input.singleNodeValue;
		if (checkbox)
			checkbox.checked = true;
	};

for (let i = 0; i < total_links; ++i) {
	const link = links.snapshotItem (i);
	link.setAttribute ('target', '_blank');
	link.addEventListener ('click', tick_submission);
}

})();
